
const Service = require('./base_service')

class FactorGroupService extends Service {
  constructor (...props) {
    super(...props)
    this.tableName = 'sys_factor_group'
    this.contentTable = 'sys_factor_group_content'
  }
  //查询列表
  index () {
    let queryString = `
      select id
      , name
      , comment
      , create_time AS "createTime"
      FROM ${this.tableName}
      ORDER BY id
    `
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then((client) => {
          client.query(queryString, (err, res) => {
            client.release()
            if (err) {
              return reject(err)
            }
            resolve(res)
          })
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
  //新建
  create (props) {
    let { name, comment } = props
    let params = [name, comment]
    let queryString = `
      INSERT INTO 
      ${this.tableName}(name, comment)
      VALUES($1, $2)
    `
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then(client => {
          client.query(
            queryString
            , params
            , (err, res) => {
              client.release()
              if (err) return reject(err)
              resolve(res)
            }
          )
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
  update (props) {
    let { id, name, comment } = props
    let params = [id, name, comment]
    let queryString = `
      UPDATE ${this.tableName} SET 
      name = $2, 
      comment = $3
      WHERE id = $1
    `
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then(client => {
          client.query(
            queryString
            , params
            , (err, res) => {
              client.release()
              if (err) return reject(err)
              resolve(res)
            }
          )
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
  delete (id) {
    let queryString = `
      DELETE FROM ${this.tableName} WHERE id = ${id}
    `
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then(client => {
          client.query(
            queryString
            , (err, res) => {
              client.release()
              if (err) return reject(err)
              resolve(res) 
            }
          )
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
  
  allFactor () {
    let queryString = `
      SELECT name,chiname 
      from imatrix_factor
      WHERE status=1
      ORDER BY order_by_name
    `
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then(client => {
          client.query(
            queryString
            , (err, res) => {
              client.release()
              if (err) return reject
              resolve(res)
            }
          )
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
  //获取某个因子组所包含的因子
  factorList (id) {
    let queryString = `
      SELECT factor_name AS factorName 
      From ${this.contentTable}
      WHERE factor_group_id=${id}
    `
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then(client => {
          client.query(
            queryString
            , (err, res) => {
              if (err) return reject(err)
              resolve(res)
            }
          )
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
  //更新某个因子组 Gid, list
  updateFactorGroup (params) {
    let { factorList, id } = params
    let deleteQuery = `
      DELETE FROM ${this.contentTable}  
      WHERE factor_group_id=${id}
    `
    factorList = JSON.parse(factorList)
    let createQuery = `
      INSERT INTO ${this.contentTable}(factor_group_id, factor_name) VALUES
    `
    factorList.forEach((factorName, index) => {
      createQuery += `${index > 0 ? ',' : ''}(${id}, '${factorName}')`
    })
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then(client => {
          client.query(
            deleteQuery
            , (err, res) => {
            client.release()
            if (err) return reject(err)
          })
          return this.postgreServer()
            .connect()
        })
        .then(client => {
          client.query(
            createQuery
            , (err, res) => {
            client.release()
            if (err) return reject(err)
            resolve(res)
          })
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
}

module.exports = FactorGroupService
